/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef NDKCREATEUI_ARKUILISTITEMADAPTER_H
#define NDKCREATEUI_ARKUILISTITEMADAPTER_H

#include <arkui/native_node.h>
#include <cstdint>
#include <stack>
#include <string>

#include "ArkUIListItemNode.h"
#include "ArkUITextNode.h"

namespace NativeModule {
class ArkUIListItemAdapter {
public:

    ArkUIListItemAdapter()
        : module_(NativeModuleInstance::GetInstance()->GetNativeNodeAPI()),
          handle_(OH_ArkUI_NodeAdapter_Create()) { // Create a function using NodeAdapter.
        // Initialize lazy data loading.
        for (int32_t i = 0; i < num; i++) {
            data_.emplace_back(std::to_string(i));
        }
        // Set lazy data loading.
        OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, data_.size());
        // Set lazy loading callback events.
        OH_ArkUI_NodeAdapter_RegisterEventReceiver(handle_, this, OnStaticAdapterEvent);
    }

    ArkUIListItemAdapter(int32_t index)
        : module_(NativeModuleInstance::GetInstance()->GetNativeNodeAPI()),
          handle_(OH_ArkUI_NodeAdapter_Create()) { // Create a function using NodeAdapter.
        // Initialize lazy data loading.
        for (int32_t i = 0; i < index; i++) {
            data_.emplace_back(std::to_string(i));
        }
        // Set lazy data loading.
        OH_ArkUI_NodeAdapter_SetTotalNodeCount(handle_, data_.size());
        // Set lazy loading callback events.
        OH_ArkUI_NodeAdapter_RegisterEventReceiver(handle_, this, OnStaticAdapterEvent);
    }

    ~ArkUIListItemAdapter()
    {
        while (!cachedItems_.empty()) {
            cachedItems_.pop();
        }
        items_.clear();
        OH_ArkUI_NodeAdapter_UnregisterEventReceiver(handle_);
        OH_ArkUI_NodeAdapter_Dispose(handle_);
    }

    ArkUI_NodeAdapterHandle GetHandle() const;

    void RemoveItem(int32_t index);
    
private:
    static void OnStaticAdapterEvent(ArkUI_NodeAdapterEvent *event);

    void OnAdapterEvent(ArkUI_NodeAdapterEvent *event);
    
    void OnNewItemIdCreated(ArkUI_NodeAdapterEvent *event);
    
    void OnNewItemAttached(ArkUI_NodeAdapterEvent *event);

    void OnItemDetached(ArkUI_NodeAdapterEvent *event);
    
    static constexpr int32_t num = 1000;
    static constexpr int FONT_SIZE = 16;
    static constexpr double PERCENT_WIDTH = 1;
    static constexpr int ITEM_HEIGHT = 100;
    static constexpr int ITEM_WIDTH = 100;

    std::vector<std::string> data_;
    ArkUI_NativeNodeAPI_1 *module_ = nullptr;
    ArkUI_NodeAdapterHandle handle_ = nullptr;

    // Manage the elements generated by NodeAdapter.
    std::unordered_map<ArkUI_NodeHandle, std::shared_ptr<ArkUIListItemNode>> items_;

    // Manage the pool of recycled and reused components.
    std::stack<std::shared_ptr<ArkUIListItemNode>> cachedItems_;
};
} // namespace NativeModule

#endif // NDKCREATEUI_ARKUILISTITEMADAPTER_H